package com.asset.dispoal.business.mediation.service.common;

import com.asset.dispoal.business.mediation.domain.db.entity.ReduceAgreementTmp;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceOperate;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceStagingTmp;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceWayTmp;
import com.asset.dispoal.business.mediation.domain.db.entity.UserRedis;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceAgreementTmpMapper;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceOperateMapper;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceStagingTmpMapper;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceWayTmpMapper;
import com.asset.dispoal.business.mediation.dto.AgreementInfo;
import com.asset.dispoal.business.mediation.dto.ReducePreInfo;
import com.asset.dispoal.business.mediation.dto.SaveReduceInfoRequest;
import com.asset.dispoal.business.mediation.dto.StagingInfo;
import com.asset.dispoal.business.mediation.service.converter.MediationConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.redis.commands.IRedisExtCommands;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: yf
 * @date: 2022-08-26 10:41:14
 */
@Component
public class SaveReduceInfoCommand implements BaseCommand<SaveReduceInfoRequest, Result> {
    private static final String PREFIX_USER_ID = "sys_user_id_";
    @Autowired
    private BaseIdeable<String> ideable;
    @Autowired
    private ReduceAgreementTmpMapper reduceAgreementTmpMapper;
    @Autowired
    private ReduceOperateMapper reduceOperateMapper;
    @Autowired
    private ReduceStagingTmpMapper reduceStagingTmpMapper;
    @Autowired
    private ReduceWayTmpMapper reduceWayTmpMapper;
    @Autowired
    private IRedisExtCommands redisExtCommands;

    @Override
    @Transactional(propagation = Propagation.NESTED)
    public Result execute(SaveReduceInfoRequest request) {
        String rat = "";
        String rwt = "";
        String rst = "";
        String numOfStag = "";

        List<AgreementInfo> agreementTmpList = request.getAgreementTmpList();
        if (!CollectionUtils.isEmpty(agreementTmpList)) {
            for (AgreementInfo agreementInfo : agreementTmpList) {
                agreementInfo.setId(ideable.generateId());
            }
            List<ReduceAgreementTmp> reduceAgreementTmps = MediationConverter.INSTANCE.reqToDoRedAg(agreementTmpList);
            //合同信息入库
            reduceAgreementTmpMapper.insertBatch(reduceAgreementTmps);

            rat = reduceAgreementTmps.stream().map(ReduceAgreementTmp::getId).collect(Collectors.joining(","));
        }


        List<ReducePreInfo> preInfos = request.getReducePreInfos();
        if (!CollectionUtils.isEmpty(preInfos)) {
            List<ReduceWayTmp> wayTmps = MediationConverter.INSTANCE.reqToDoRedWt(preInfos);
            for (ReduceWayTmp wayTmp : wayTmps) {
                wayTmp.setId(ideable.generateId());
            }

            //减免信息入库
            reduceWayTmpMapper.insertBatch(wayTmps);
            rwt = wayTmps.stream().map(ReduceWayTmp::getId).collect(Collectors.joining(","));
        }

        List<StagingInfo> stagingInfos = request.getStagingInfos();
        if (!CollectionUtils.isEmpty(stagingInfos)) {
            List<ReduceStagingTmp> stagingTmps = MediationConverter.INSTANCE.reqToDoRedSt(stagingInfos);
            for (ReduceStagingTmp stagingTmp : stagingTmps) {
                stagingTmp.setId(ideable.generateId());
            }
            //分期信息入库
            reduceStagingTmpMapper.insertBatch(stagingTmps);
            rst = stagingTmps.stream().map(ReduceStagingTmp::getId).collect(Collectors.joining(","));
            numOfStag = String.valueOf(stagingTmps.size());
        }


        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        UserRedis user = redisExtCommands.hget(PREFIX_USER_ID, userId, UserRedis.class);

        ReduceOperate operate = new ReduceOperate();
        operate.setCaseId(request.getCaseId());
        operate.setId(ideable.generateId());
        operate.setAgreementIds(rat);
        operate.setStagingIds(rst);
        operate.setReduceIds(rwt);
        operate.setOperateTime(new Date());
        operate.setOperateUser(user.getAccount());
        operate.setNumOfStaging(numOfStag);

        reduceOperateMapper.insert(operate);
        return Result.ok();
    }
}
